package com.iwown.sample4GApi.service;

import com.google.protobuf.InvalidProtocolBufferException;
import com.iwown.sample4GApi.protobuf.AlarmInfo;
import com.iwown.sample4GApi.protobuf.RealtimeData;
import com.iwown.sample4GApi.utils.MyDateTimeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class NewAlarmDataParser implements IDataParser {
    private static final Logger log = LoggerFactory.getLogger(NewAlarmDataParser.class);
    @Override
    public void DataParse(byte[] payload) {
        AlarmInfo.Alarm_infokConfirm alarm_infokConfirm = null;
        try {
            alarm_infokConfirm = AlarmInfo.Alarm_infokConfirm.parseFrom(payload);
        } catch(InvalidProtocolBufferException ex) {
            ex.printStackTrace();
            return;
        }
        if(alarm_infokConfirm.hasAlarm()){
            AlarmInfo.HealthAlarmV3 alarmV3 = alarm_infokConfirm.getAlarm();

            //sos alarm for device has no phone feature
            List<AlarmInfo.SOSGNSSINFOV3> sosAlarmList = alarmV3.getGnssinfoList();
            if(sosAlarmList!=null && sosAlarmList.size()>0){
                for(int i=0;i<sosAlarmList.size();i++){
                    AlarmInfo.SOSGNSSINFOV3 sosAlarm = sosAlarmList.get(i);
                    String alarmTime = MyDateTimeUtils.convert2DateTimeString(sosAlarm.getTimeStamp().getDateTime().getSeconds());
                    log.info(String.format("sos alarm time:%s, lat:%f,lon:%f,loc type:%d",alarmTime,sosAlarm.getLatitude(),sosAlarm.getLongitude(),sosAlarm.getGpsType().ordinal()));
                }
            }

            //hr warn
            List<AlarmInfo.HrAlarmV3> hrAlarmList = alarmV3.getAlarmHrList();
            if(hrAlarmList!=null && hrAlarmList.size()>0){
                for(int i=0;i<hrAlarmList.size();i++){
                    AlarmInfo.HrAlarmV3 hrAlarm = hrAlarmList.get(i);
                    String alarmTime = MyDateTimeUtils.convert2DateTimeString(hrAlarm.getTimeStamp().getDateTime().getSeconds());
                    log.info(String.format("hr warn time:%s, hr:%d",alarmTime,hrAlarm.getHr()));
                }
            }

            //spo2 warn
            List<AlarmInfo.Spo2AlarmV3> spo2AlarmList = alarmV3.getAlarmSpo2List();
            if(spo2AlarmList!=null && spo2AlarmList.size()>0){
                for(int i=0;i<spo2AlarmList.size();i++){
                    AlarmInfo.Spo2AlarmV3 spo2Alarm = spo2AlarmList.get(i);
                    String alarmTime = MyDateTimeUtils.convert2DateTimeString(spo2Alarm.getTimeStamp().getDateTime().getSeconds());
                    log.info(String.format("spo2 alarm time:%s, boxy:%d",alarmTime,spo2Alarm.getSpo2()));
                }
            }

            //thrombus warn
            List<AlarmInfo.ThrombusAlarmV3> thrombusAlarmList = alarmV3.getAlarmThrombusList();
            if(thrombusAlarmList!=null && thrombusAlarmList.size()>0){
                for(int i=0;i<thrombusAlarmList.size();i++){
                    AlarmInfo.ThrombusAlarmV3 thrombusAlarm = thrombusAlarmList.get(i);
                    String alarmTime = MyDateTimeUtils.convert2DateTimeString(thrombusAlarm.getTimeStamp().getDateTime().getSeconds());
                    log.info(String.format("thrombus alarm time:%s",alarmTime));
                }
            }

            //the guy fall down
            List<AlarmInfo.FallAlarmConfV3> fallAlarmList = alarmV3.getAlarmFallList();
            if(fallAlarmList!=null && fallAlarmList.size()>0){
                for(int i=0;i<fallAlarmList.size();i++){
                    AlarmInfo.FallAlarmConfV3 fallAlarm = fallAlarmList.get(i);
                    String alarmTime = MyDateTimeUtils.convert2DateTimeString(fallAlarm.getTimeStamp().getDateTime().getSeconds());
                    log.info(String.format("fall down alarm time:%s",alarmTime));
                }
            }

            //temperature alarm
            List<AlarmInfo.TemperatureConf> tempAlarmList = alarmV3.getAlarmTemperatureList();
            if(tempAlarmList!=null && tempAlarmList.size()>0){
                for(int i=0;i<tempAlarmList.size();i++){
                    AlarmInfo.TemperatureConf tempAlarm = tempAlarmList.get(i);
                    String alarmTime = MyDateTimeUtils.convert2DateTimeString(tempAlarm.getTimeStamp().getDateTime().getSeconds());
                    log.info(String.format("temperature alarm time:%s,temperature:%d",alarmTime,tempAlarm.getTemperature()));
                }
            }

            //bp alarm
            List<AlarmInfo.BpConf> bpAlarmList = alarmV3.getAlarmBpList();
            if(bpAlarmList!=null && bpAlarmList.size()>0){
                for(int i=0;i<bpAlarmList.size();i++){
                    AlarmInfo.BpConf bpAlarm = bpAlarmList.get(i);
                    String alarmTime = MyDateTimeUtils.convert2DateTimeString(bpAlarm.getTimeStamp().getDateTime().getSeconds());
                    log.info(String.format("bp alarm time:%s,sbp:%d,dbp:%d",alarmTime,bpAlarm.getSbp(),bpAlarm.getDbp()));
                }
            }

            //sos alarm, will send an alarm without location first, before do locate/call
            if(alarmV3.hasSOSNotificationTime()){
                RealtimeData.DateTime sosTime = alarmV3.getSOSNotificationTime();
                String alarmTime = MyDateTimeUtils.convert2DateTimeString(sosTime.getDateTime().getSeconds());
                log.info(String.format("sos alarm time:%s",alarmTime));
            }

            //blood potassium alarm
            List<AlarmInfo.Blood_potassium_Conf> potassiumAlarmList = alarmV3.getAlarmBloodPotassiumList();
            if(potassiumAlarmList!=null && potassiumAlarmList.size()>0){
                for(int i=0;i<potassiumAlarmList.size();i++){
                    AlarmInfo.Blood_potassium_Conf postassiumAlarm = potassiumAlarmList.get(i);
                    String alarmTime = MyDateTimeUtils.convert2DateTimeString(postassiumAlarm.getTimeStamp().getDateTime().getSeconds());
                    log.info(String.format("blood potassium alarm time:%s,potassium:%f",alarmTime,postassiumAlarm.getBloodPotassium()));
                }
            }

            //blood sugar alarm
            List<AlarmInfo.Blood_sugar_Conf> sugarAlarmList = alarmV3.getAlarmBloodSugarList();
            if(sugarAlarmList!=null && sugarAlarmList.size()>0){
                for(int i=0;i<sugarAlarmList.size();i++){
                    AlarmInfo.Blood_sugar_Conf sugarAlarm = sugarAlarmList.get(i);
                    String alarmTime = MyDateTimeUtils.convert2DateTimeString(sugarAlarm.getTimeStamp().getDateTime().getSeconds());
                    log.info(String.format("blood sugar alarm time:%s,bloodsugar:%f",alarmTime,sugarAlarm.getBloodSugar()));
                }
            }
        }
        if(alarm_infokConfirm.hasAlarminfo()){
            AlarmInfo.AlarminfoV3 infoV3 = alarm_infokConfirm.getAlarminfo();
            String alarmTime = MyDateTimeUtils.convert2DateTimeString(infoV3.getTimeStamp().getDateTime().getSeconds());
            //device in low power
            if(infoV3.hasLowpowerPercentage()){
                log.info(String.format("low power time:%s, battery:%d",alarmTime,infoV3.getLowpowerPercentage()));
            }
            //device is power off
            if(infoV3.hasPoweroffPercentage()){
                log.info(String.format("power off time:%s, battery:%d",alarmTime,infoV3.getPoweroffPercentage()));
            }
            //device not wear
            if(infoV3.hasWearstate()){
                log.info(String.format("not wear time:%s",alarmTime));
            }
            //intercept call
            if(infoV3.hasInterceptNumber()){
                log.info(String.format("intercept call time:%s, phone number:%s",alarmTime,infoV3.getInterceptNumber()));
            }
        }
    }
}
